
package io.renren.modules.jxgkphone.controller;


import io.renren.common.utils.R;
import io.renren.modules.jxgkphone.entity.UserInfo;
import io.renren.modules.jxgkphone.service.PasswordModifyService;
import io.renren.modules.jxgkphone.tempEntity.TempPasswordModify;
import io.renren.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/password")
public class PasswordModifyController {
    @Autowired
    PasswordModifyService passwordModifyService;
    @Autowired
    UserInfo userInfo;

    @RequestMapping("/PasswordModify")
    public String PasswordModify(@RequestBody TempPasswordModify tempPasswordModify){
        try {
            userInfo.setId(Integer.parseInt(tempPasswordModify.getId()));
            userInfo.setPassword(tempPasswordModify.getOldPassword());
            passwordModifyService.PasswordModify(userInfo.getId()+"", userInfo.getPassword(), tempPasswordModify.getNewPassword());
            System.out.println("更改密码成功");
            return "更改密码成功";
        }
        catch (Exception e){
            System.out.println("更改密码失败");
            return "更改密码失败";
        }
    }


    @RequestMapping("/RetrievePassword")
    public R RetrievePassword(@RequestBody SysUserEntity sysUserEntity)
    {

        try {
            SysUserEntity s = passwordModifyService.getSysUserByUsernameAndEmail(sysUserEntity.getUsername(), sysUserEntity.getEmail());
            if(s==null||sysUserEntity.getUsername().equals("admin"))
            {
                return R.ok().put("msg","您输入的用户名或者Email有误，请重新输入");

            }else
            {
                passwordModifyService.retrievePassword(s);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return R.ok().put("msg","密码重置成功,请登录邮箱查收");
    }


}

